package com.baroxtech.anoterecorder;

import android.bluetooth.BluetoothDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

class ABLEDevAdapter extends RecyclerView.Adapter<ABLEDevAdapter.VH> {
    private List<BluetoothDevice> bleDeviceList;
    private EventListener eventListener;

    public static final int CLEAR_ALL = 0;
    public static final int CLEAR_CONNECTED = 1;
    public static final int CLEAR_UNCONNECTED = 2;

    public static class VH extends RecyclerView.ViewHolder {
        public final TextView tvDevName;
        public final TextView tvMacAddr;
        public final ImageView imgConnectionState;

        public VH(View v) {
            super(v);
            tvDevName = (TextView) v.findViewById(R.id.tv_dev_name);
            tvMacAddr = (TextView) v.findViewById(R.id.tv_mac_addr);
            imgConnectionState = (ImageView) v.findViewById(R.id.img_connection_state);
        }

        public void setDevice(BluetoothDevice dev) {
            if (dev == null) return;
            // 设置名字
            String name = dev.getName();
            if (name.isEmpty()) {
                tvDevName.setText(R.string.no_name_dev);
            } else {
                tvDevName.setText(name);
            }
            // 设置 mac 地址
            tvMacAddr.setText(dev.getAddress());
            // 连接状态 icon
            int connectionStateImg = AMIDIDevManager.getInstance().isConnected(new AMIDIDev(dev)) ? R.drawable.ic_link : R.drawable.ic_unlink;
            imgConnectionState.setImageResource(connectionStateImg);
        }
    }

    public interface EventListener {
        void onConnect(BluetoothDevice bleDevice);

        void onDisConnect(BluetoothDevice bleDevice);
    }

    public ABLEDevAdapter(@NonNull EventListener eventListener) {
        this.eventListener = eventListener;
        // 获取已经链接的
        AMIDIDev dev = AMIDIDevManager.getInstance().getConnectedDevice();
        bleDeviceList = new ArrayList<>();
        if (dev != null && !dev.isUsb()) {
            bleDeviceList.add(dev.bleDev);
            notifyDataSetChanged();
        }
    }

    public int add(BluetoothDevice dev) {
        int i = find(dev);
        if (i >= 0) return -1;

        bleDeviceList.add(dev);
        return bleDeviceList.size() - 1;
    }

    public int remove(BluetoothDevice dev) {
        int i = find(dev);
        if (i < 0) return i;

        bleDeviceList.remove(i);
        return i;
    }

    public boolean clear(int which) {
        boolean removed = false;

        if (which == CLEAR_ALL) {
            removed = bleDeviceList.size() > 0;
            bleDeviceList.clear();
            return removed;
        }

        boolean clearConnected = CLEAR_CONNECTED == which;
        for (int i = 0; i < bleDeviceList.size(); i++) {
            BluetoothDevice device = bleDeviceList.get(i);
            if (clearConnected == AMIDIDevManager.getInstance().isConnected(new AMIDIDev(device))) {
                bleDeviceList.remove(i);
                removed = true;
            }
        }
        return removed;
    }

    public void notifyItemChanged(final BluetoothDevice dev) {
        int i = find(dev);
        if (i < 0) return;
        notifyItemChanged(i);
    }

    private int find(final BluetoothDevice dev) {
        BluetoothDevice bd;
        for (int i = 0; i < bleDeviceList.size(); i++) {
            bd = bleDeviceList.get(i);
            if (dev.equals(bd)) {
                return i;
            }
        }
        return -1;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout, parent, false);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final VH holder, int position) {
        holder.setDevice(bleDeviceList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (eventListener == null) return;

                int pos = holder.getLayoutPosition();
                BluetoothDevice dev = bleDeviceList.get(pos);
                if (AMIDIDevManager.getInstance().isConnected(new AMIDIDev(dev))) {
                    eventListener.onDisConnect(dev);
                } else {
                    eventListener.onConnect(dev);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return bleDeviceList.size();
    }
}
